package com.midea.ai.air.ui.home.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.midea.ai.air.R;
import com.midea.ai.air.common.BaseFragment;
import com.midea.ai.air.ui.home.adapter.DeviceManagerAdapter;
import com.midea.ai.air.ui.home.data.DeviceManager;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ouyangdi on 2016/4/9.
 * 编辑房间->家电管理Fragment
 */
public class EditDeviceManagerFragment extends BaseFragment {
    private ListView mDeviceList;
    private List<DeviceManager> mDevices = new ArrayList<>();
    private DeviceManagerAdapter mDeviceManagerAdapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_edit_device_manager, container, false);
        initData();
        initView(view);
        return view;
    }

    private void initData() {
        mDevices.add(new DeviceManager("空调", "KJ30FE", true));
        mDevices.add(new DeviceManager("空调", "KJ30FE", false));
        mDevices.add(new DeviceManager("空调", "KJ30FE", false));
        mDevices.add(new DeviceManager("空调", "KJ30FE", false));

        mDevices.add(null);

        mDevices.add(new DeviceManager("净化器", "KJ30FE", false));

        mDevices.add(null);

        mDevices.add(new DeviceManager("加湿器", "KJ30FE", true));

        mDevices.add(null);

        mDevices.add(new DeviceManager("除湿机", "KJ30FE", true));

        mDeviceManagerAdapter = new DeviceManagerAdapter(getActivity(), 0, mDevices);
    }

    private void initView(View view) {
        mDeviceList = (ListView)view.findViewById(R.id.list_device_manager);
        mDeviceList.setAdapter(mDeviceManagerAdapter);

        mDeviceList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mDevices.get(position).setIsDeviceOn(!(mDevices.get(position).isDeviceOn()));
                mDeviceManagerAdapter.notifyDataSetChanged();
            }
        });
    }
}
